/**
 *
 */
package com.iot.ic.modules.web;

import com.iot.ic.common.web.BaseController;
import com.iot.ic.modules.model.Accounts;
import com.iot.ic.modules.model.Amounts;
import com.iot.ic.modules.model.User;
import com.iot.ic.modules.service.AmountService;
import com.iot.ic.modules.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author haojinyu
 * 用户的账户和账目明细
 */
@Controller
public class AmountController extends BaseController {

    @Autowired
    private AmountService amountService;
    @Autowired
    private UserService userService;

    @RequestMapping("/patient/amount/findAmount")
    @ResponseBody
    public Map<String, Object> patientAmountUid(Integer userId) {
        return findAmountByUid(userId, 0);
    }
    @RequestMapping("/doctor/amount/findAmount")
    @ResponseBody
    public Map<String, Object> doctorAmountUid(Integer userId) {
        return findAmountByUid(userId, 1);
    }

    @RequestMapping("/patient/amount/findAccount")
    @ResponseBody
    public Map<String, Object> doctorAccountByUid(Integer userId) {
        return findAccountByUid(userId, 0);
    }
    @RequestMapping("/doctor/amount/findAccount")
    @ResponseBody
    public Map<String, Object> patientAccountByUid(Integer userId) {
        return findAccountByUid(userId, 1);
    }

    @RequestMapping("/doctor/amount/updateState")
    @ResponseBody
    public Map<String, Object> updateDrAmountState(Integer userId,Integer state) {
        return updateState(userId, state,1);
    }

    @RequestMapping("/patient/amount/updateState")
    @ResponseBody
    public Map<String, Object> updatePatientAmountState(Integer userId,Integer state) {
        return updateState(userId, state,0);
    }
    /**充值*/
    @RequestMapping("/patient/amount/rechange")
    @ResponseBody
    public Map<String, Object> rechange(Integer userId,Integer state) {
        //TODO 充值功能
        Map<String, Object> map =  packInfo(false,"充值功能尚在开发.....");
        map.put("userId",userId);
        map.put("state",state);
        return map;
    }
    /**提现*/
    @RequestMapping("/doctor/amount/takeCash")
    @ResponseBody
    public Map<String, Object> takeCash(Integer userId,Integer state) {
        //TODO  提现功能
         Map<String, Object> map =  packInfo(false,"提现功能尚在开发.....");
        map.put("userId",userId);
        map.put("state",state);
        return map ;
    }


    /**
     * 查询用户的金额
     * @param userId 用户编号
      * @param userType 用户类型 0病人 1 医生
     * @return Map json
     */
    private Map<String, Object> findAmountByUid(Integer userId, int userType) {
        if (StringUtils.isEmpty(userId)) {
            return packInfo(false, "userId为空，查询");
        }
        try {
            User user = userService.findById(userId, userType);
            if (user == null) {
                return packInfo(false, "用户不存在，查询");
            }
            Amounts amount = amountService.findAmountByUid(userId);
            return packData(amount);
        } catch (Exception e) {
            e.printStackTrace();
            return warnException(e);
        }
    }
    /**
     * 查询用户账户状态
     * @param userId 用户编号
      * @param userType 用户类型 0病人 1 医生
      * @param state 用户类型 0正常， 1 注销 ，2 冻结
     * @return Map json
     */
    private Map<String, Object> updateState(Integer userId, Integer state, int userType) {
        if (StringUtils.isEmpty(userId)) {
            return packInfo(false, "userId为空，保存","userId");
        }
        if (StringUtils.isEmpty(state)) {
            return packInfo(false, "state 为空，保存","state");
        }
        try {
            User user = userService.findById(userId, userType);
            if (user == null) {
                return packInfo(false, "用户不存在，保存");
            }
            Amounts amount = amountService.updateAmountState(userId,state);
            if(amount!=null){
                Map<String, Object> map = packData(amount);
                map.put("message", "保存成功！");
                return map;
            }else{
                return packInfo(false, "未知错误，保存");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return warnException(e);
        }
    }

    public Map<String, Object> findAccountByUid(Integer userId,int userType) {
        if (StringUtils.isEmpty(userId)) {
            return packInfo(false, "用户名编号为空，查询","userId");
        }
        try {
            User user = userService.findById(userId, userType);
            if (user == null) {
                return packInfo(false, "用户不存在，查询");
            }
            List<Accounts> accounts = amountService.findAccountByUid(userId);
            return packData(accounts);
        } catch (Exception e) {
            e.printStackTrace();
            return warnException(e);
        }
    }
}
